package cn.liuhuan1.blogsModel.service;


import cn.liuhuan1.api.RedisClient;
import cn.liuhuan1.blogsModel.dao.WebUserDao;
import cn.liuhuan1.entity.ReturnResult;
import cn.liuhuan1.webModelBean.api.WebUserImp;
import cn.liuhuan1.webModelBean.vo.WebUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WebUserService implements WebUserImp {

    @Autowired
    public WebUserDao webUserDao;

    @Autowired
    private RedisClient redisClient;

    @Override
    public List<WebUserVo> getWebUser(){
        List<WebUserVo> webUserVoList = webUserDao.getWebUser();
        return webUserVoList;
    }

    @Override
    public List<WebUserVo> getInId(List<String> idList) {
       return webUserDao.getInId(idList);
    }

    @Override
    public WebUserVo getById(String id) {
        return webUserDao.getById(id);
    }

    @Override
    public ReturnResult register(WebUserVo webUserVo, String authCode) {
        if(webUserVo==null || webUserVo.getEmail() == null || webUserVo.getPassword() == null || webUserVo.getPassword().trim().length() < 1 || webUserVo.getEmail().trim().length() < 1){
            return ReturnResult.error("500","注册失败，密码或邮箱为null");
        }
        if(authCode == null || authCode.trim().length() < 1){
            return ReturnResult.error("500","验证码错误");
        }
        String value = redisClient.getValue(webUserVo.getEmail());
        if(value == null || value.trim().length() < 1){
            return ReturnResult.error("500","验证码已失效");
        }
        System.out.println(value.toUpperCase());
        if(authCode == null || !authCode.toUpperCase().equals(value.toUpperCase())){
            return ReturnResult.error("500","验证码错误或者已失效");
        }
        webUserDao.insert(webUserVo);
        return ReturnResult.succes();
    }

    @Override
    public ReturnResult loginWebUser(String mail, String password) {
        if(mail == null || password == null || mail.trim().length() < 1 || password.trim().length() < 1){
            return ReturnResult.error("500","账号密码 不能为空");
        }
        List<WebUserVo> webUserVoList = webUserDao.getByMailAndPassword(mail, password);
        if(webUserVoList == null || webUserVoList.size() < 1){
            return ReturnResult.error("500","账号密码错误");
        }

        return ReturnResult.succes(webUserVoList.get(0));
    }

    @Override
    public ReturnResult getByMail(String mail) {
        if(mail == null || mail.trim().length() < 1){
            return ReturnResult.error("500","邮箱错误");
        }
        List<WebUserVo> userVoList = webUserDao.getByMail(mail);
        if(userVoList == null || userVoList.size() < 0){
            return ReturnResult.error("500","邮箱错误");
        }
        return ReturnResult.succes();
    }
}
